package com.oriental.manage.service.reserve;

import com.oriental.manage.core.exception.BusiException;
import com.oriental.manage.core.paging.Pagination;
import com.oriental.manage.core.response.ResponseDTO;
import com.oriental.manage.core.utils.SessionUtils;
import com.oriental.reserve.api.report.ReportBaseDataInterface;
import com.oriental.reserve.api.trans.AdjustmentDetailInterface;
import com.oriental.reserve.enums.AdjustmentStatus;
import com.oriental.reserve.model.ResponseModel;
import com.oriental.reserve.model.report.ReportBaseDataDto;
import com.oriental.reserve.model.trans.AdjustmentDetailDto;
import com.oriental.reserve.model.trans.AdjustmentMonth110ReportDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * Author: Yang xp
 * Date: 2017/3/2
 * Time: 13:56
 * Desc：
 */
@Service
@Slf4j
public class ReportAdjustmentService {

    @Autowired
    private AdjustmentDetailInterface adjustmentDetailInterface;

    @Autowired
    private ReportBaseDataInterface reportBaseDataInterface;

    public void queryAdjustment(ResponseDTO<Pagination<AdjustmentDetailDto>> responseDTO,Pagination<AdjustmentDetailDto> pagination, AdjustmentDetailDto adjustmentDetailDto){
        log.info("调整数据查询:{}",adjustmentDetailDto);
        ResponseModel<List<AdjustmentDetailDto>> responseModel = adjustmentDetailInterface.selectAdjustmentDetail(adjustmentDetailDto);
        ReserveUtil.responseQueryHandle(responseModel,pagination,responseDTO,adjustmentDetailDto,"查询失败");
    }

    public void queryReportBaseData(ResponseDTO<Pagination<ReportBaseDataDto>> responseDTO, Pagination<ReportBaseDataDto> pagination, ReportBaseDataDto reportBaseDataDto){
        log.info("基础数据查询:{}",reportBaseDataDto);
        ResponseModel<List<ReportBaseDataDto>> responseModel = reportBaseDataInterface.selectReportBaseData(reportBaseDataDto);
        ReserveUtil.responseQueryHandle(responseModel,pagination,responseDTO,reportBaseDataDto,"查询失败");
    }

    public void createAdjustment(ResponseDTO<String> responseDTO,AdjustmentDetailDto adjustmentDetailDto){
        log.info("调整信息新增:{}",adjustmentDetailDto);
        adjustmentDetailDto.setHandledBy(SessionUtils.getUserName());
        adjustmentDetailDto.setHandledAt(new Date());
        adjustmentDetailDto.setCreateBy(SessionUtils.getUserName());
        ResponseModel<String> responseModel = adjustmentDetailInterface.createAdjustmentDetail(adjustmentDetailDto);
        ReserveUtil.responseHandle(responseModel,responseDTO,"新增成功");
    }

    public void updateAdjustment(ResponseDTO<String> responseDTO,AdjustmentDetailDto adjustmentDetailDto){
        log.info("调整信息修改:{}",adjustmentDetailDto);
        adjustmentDetailDto.setUpdateBy(SessionUtils.getUserName());
        ResponseModel<String> responseModel = adjustmentDetailInterface.updateAdjustmentDetail(adjustmentDetailDto);
        ReserveUtil.responseHandle(responseModel,responseDTO,"修改成功");
    }

    public void deleteAdjustment(ResponseDTO<String> responseDTO,AdjustmentDetailDto adjustmentDetailDto){
        log.info("调整信息删除:{}",adjustmentDetailDto);
        adjustmentDetailDto.setUpdateBy(SessionUtils.getUserName());
        ResponseModel<String> responseModel = adjustmentDetailInterface.deleteAdjustmentDetail(adjustmentDetailDto);
        ReserveUtil.responseHandle(responseModel,responseDTO,"删除成功");
    }

    public void reviewPass(ResponseDTO<String> responseDTO,List<AdjustmentDetailDto> adjustmentDetailDtoList){
        log.info("调整审核通过:{}",adjustmentDetailDtoList);
        for (AdjustmentDetailDto adjustmentDetailDto : adjustmentDetailDtoList){
            if (!AdjustmentStatus.INIT.getCode().equals(adjustmentDetailDto.getAdjustmentStatus())){
                throw new BusiException("","状态不为初始不能操作");
            }
            adjustmentDetailDto.setReviewBy(SessionUtils.getUserName());
            adjustmentDetailDto.setReviewAt(new Date());
            adjustmentDetailDto.setUpdateBy(SessionUtils.getUserName());
            adjustmentDetailDto.setAdjustmentStatus(AdjustmentStatus.REVIEW_PASS.getCode());
        }
        ResponseModel<String> responseModel = adjustmentDetailInterface.reviewPass(adjustmentDetailDtoList);
        ReserveUtil.responseHandle(responseModel,responseDTO,"操作成功");
    }

    public void reviewFail(ResponseDTO<String> responseDTO,List<AdjustmentDetailDto> adjustmentDetailDtoList){
        log.info("调整审核拒绝:{}",adjustmentDetailDtoList);
        for (AdjustmentDetailDto adjustmentDetailDto : adjustmentDetailDtoList){
            if (!AdjustmentStatus.INIT.getCode().equals(adjustmentDetailDto.getAdjustmentStatus())){
                throw new BusiException("","状态不为初始不能操作");
            }
            adjustmentDetailDto.setReviewBy(SessionUtils.getUserName());
            adjustmentDetailDto.setReviewAt(new Date());
            adjustmentDetailDto.setUpdateBy(SessionUtils.getUserName());
            adjustmentDetailDto.setAdjustmentStatus(AdjustmentStatus.REVIEW_FAIL.getCode());
        }
        ResponseModel<String> responseModel = adjustmentDetailInterface.reviewFail(adjustmentDetailDtoList);
        ReserveUtil.responseHandle(responseModel,responseDTO,"操作成功");
    }

    public void reviewBack(ResponseDTO<String> responseDTO,List<AdjustmentDetailDto> adjustmentDetailDtoList){
        log.info("调整审核回退:{}",adjustmentDetailDtoList);
        for (AdjustmentDetailDto adjustmentDetailDto : adjustmentDetailDtoList){
            if (!AdjustmentStatus.REVIEW_PASS.getCode().equals(adjustmentDetailDto.getAdjustmentStatus())){
                throw new BusiException("","状态不为审核通过不能操作");
            }
            adjustmentDetailDto.setReviewBy(SessionUtils.getUserName());
            adjustmentDetailDto.setReviewAt(new Date());
            adjustmentDetailDto.setUpdateBy(SessionUtils.getUserName());
            adjustmentDetailDto.setAdjustmentStatus(AdjustmentStatus.REVIEW_BACK.getCode());
        }
        ResponseModel<String> responseModel = adjustmentDetailInterface.reviewBack(adjustmentDetailDtoList);
        ReserveUtil.responseHandle(responseModel,responseDTO,"操作成功");
    }



    public void queryAdjustment(ResponseDTO<Pagination<AdjustmentMonth110ReportDto>> responseDTO,Pagination<AdjustmentMonth110ReportDto> pagination, AdjustmentMonth110ReportDto adjustmentDetailDto){
        log.info("调整数据查询:{}",adjustmentDetailDto);
        ResponseModel<List<AdjustmentMonth110ReportDto>> responseModel = adjustmentDetailInterface.selectAdjustment110Detail(adjustmentDetailDto);
        ReserveUtil.responseQueryHandle(responseModel,pagination,responseDTO,adjustmentDetailDto,"查询失败");
    }


    public void createAdjustment(ResponseDTO<String> responseDTO,AdjustmentMonth110ReportDto adjustmentDetailDto){
        log.info("调整信息新增:{}",adjustmentDetailDto);
        adjustmentDetailDto.setHandledBy(SessionUtils.getUserName());
        adjustmentDetailDto.setHandledAt(new Date());
        adjustmentDetailDto.setCreateBy(SessionUtils.getUserName());
        ResponseModel<String> responseModel = adjustmentDetailInterface.createAdjustment110Detail(adjustmentDetailDto);
        ReserveUtil.responseHandle(responseModel,responseDTO,"新增成功");
    }

    public void updateAdjustment(ResponseDTO<String> responseDTO,AdjustmentMonth110ReportDto adjustmentDetailDto){
        log.info("调整信息修改:{}",adjustmentDetailDto);
        adjustmentDetailDto.setUpdateBy(SessionUtils.getUserName());
        ResponseModel<String> responseModel = adjustmentDetailInterface.updateAdjustment110Detail(adjustmentDetailDto);
        ReserveUtil.responseHandle(responseModel,responseDTO,"修改成功");
    }

    public void deleteAdjustment(ResponseDTO<String> responseDTO,AdjustmentMonth110ReportDto adjustmentDetailDto){
        log.info("调整信息删除:{}",adjustmentDetailDto);
        adjustmentDetailDto.setUpdateBy(SessionUtils.getUserName());
        ResponseModel<String> responseModel = adjustmentDetailInterface.deleteAdjustmentDe110tail(adjustmentDetailDto);
        ReserveUtil.responseHandle(responseModel,responseDTO,"删除成功");
    }


    public void reviewPass110(ResponseDTO<String> responseDTO,List<AdjustmentMonth110ReportDto> adjustmentDetailDtoList){
        log.info("调整审核通过:{}",adjustmentDetailDtoList);
        for (AdjustmentMonth110ReportDto adjustmentDetailDto : adjustmentDetailDtoList){
            if (!AdjustmentStatus.INIT.getCode().equals(adjustmentDetailDto.getAdjustmentStatus())){
                throw new BusiException("","状态不为初始不能操作");
            }
            adjustmentDetailDto.setReviewBy(SessionUtils.getUserName());
            adjustmentDetailDto.setReviewAt(new Date());
            adjustmentDetailDto.setUpdateBy(SessionUtils.getUserName());
            adjustmentDetailDto.setAdjustmentStatus(AdjustmentStatus.REVIEW_PASS.getCode());
        }
        ResponseModel<String> responseModel = adjustmentDetailInterface.reviewPass110(adjustmentDetailDtoList);
        ReserveUtil.responseHandle(responseModel,responseDTO,"操作成功");
    }

    public void reviewFail110(ResponseDTO<String> responseDTO,List<AdjustmentMonth110ReportDto> adjustmentDetailDtoList){
        log.info("调整审核拒绝:{}",adjustmentDetailDtoList);
        for (AdjustmentMonth110ReportDto adjustmentDetailDto : adjustmentDetailDtoList){
            if (!AdjustmentStatus.INIT.getCode().equals(adjustmentDetailDto.getAdjustmentStatus())){
                throw new BusiException("","状态不为初始不能操作");
            }
            adjustmentDetailDto.setReviewBy(SessionUtils.getUserName());
            adjustmentDetailDto.setReviewAt(new Date());
            adjustmentDetailDto.setAdjustmentStatus(AdjustmentStatus.REVIEW_FAIL.getCode());
            adjustmentDetailDto.setUpdateBy(SessionUtils.getUserName());
        }
        ResponseModel<String> responseModel = adjustmentDetailInterface.reviewFail110(adjustmentDetailDtoList);
        ReserveUtil.responseHandle(responseModel,responseDTO,"操作成功");
    }

    public void reviewBack110(ResponseDTO<String> responseDTO,List<AdjustmentMonth110ReportDto> adjustmentDetailDtoList){
        log.info("调整审核回退:{}",adjustmentDetailDtoList);
        for (AdjustmentMonth110ReportDto adjustmentDetailDto : adjustmentDetailDtoList){
            if (!AdjustmentStatus.REVIEW_PASS.getCode().equals(adjustmentDetailDto.getAdjustmentStatus())){
                throw new BusiException("","状态不为审核通过不能操作");
            }
            adjustmentDetailDto.setReviewAt(new Date());
            adjustmentDetailDto.setReviewBy(SessionUtils.getUserName());
            adjustmentDetailDto.setUpdateBy(SessionUtils.getUserName());
            adjustmentDetailDto.setAdjustmentStatus(AdjustmentStatus.REVIEW_BACK.getCode());
        }
        ResponseModel<String> responseModel = adjustmentDetailInterface.reviewBack110(adjustmentDetailDtoList);
        ReserveUtil.responseHandle(responseModel,responseDTO,"操作成功");
    }

}
